<?php
namespace app\admin\controller;

use app\admin\model\AuthRule as RuleModel;
use my\Tree;
use Request;

class AuthRule extends Base
{

   public function index($value = '')
    {
        // $list = RuleModel::select(); //限定后台用户
        // $tree = new Tree();
        // $data = $tree->tree($list, 'title');
        // $this->assign([
        //     'list' => $data,
        // ]);
        return $this->fetch();
    }

    public function ajaxlist($page = 1, $limit = '')
    {
        $val   = !empty($val) ? trim($val) : '';
        $limit = !empty($limit) ? trim($limit) : config('webconfig.page');
        $page  = !empty($page) ? $page : 1;

        $list  = RuleModel::where('module', 'admin')->order('sorts', 'desc')->page($page, $limit)->select();
        $count = RuleModel::where('module', 'admin')->count();
         $tree = new Tree();
        $data = $tree->tree($list, 'title');
        foreach ($data as $k => $v) {
            $data[$k]['icon'] = $v['icon'];
        }
        $list = ["code" => 0, "msg" => "加载中", "count" => $count, "data" => array_values($data)];
        return json($list);
    }

    /*
    信息添加
     */
    public function add()
    {
        //$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\AuthRule');
            if (true !== $result) {
                return $this->error($result);
            }

            $data = RuleModel::create($post);
            if (!empty($data)) {
                return $this->success('添加成功', 'admin/AuthRule/index');
            }
        }

        // $info = RuleModel::order('sorts,id', 'desc')->find($id);
        // if(empty($info)){return $this->error('id参数错误');}

        $data = RuleModel::order('sorts,id,module', 'desc')->select();
        $tree = new Tree();
        $data = $tree->tree($data, 'title');
        $this->assign([
            'typename' => $data,
            //'typepid'=>$info['id'],
        ]);
        return $this->fetch();
    }

    /*
    信息编辑
     */
    public function edit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {

            $post = $this->request->post();
            //print_r($post);
            $result = $this->validate($post, 'app\admin\validate\AuthRule.edit');
            if (true !== $result) {
                return $this->error($result);
            }

            $model = new RuleModel;
            $model = $model->allowField(true)->save($post, $id);

            if ($model) {
                return $this->success('修改成功', 'admin/AuthRule/index');
            } else {
                return $this->error('修改失败');
            }
        }

        $info = RuleModel::order('sorts,id,module', 'desc')->find($id);
        if (empty($info)) {return $this->error('id参数错误');}
        $data = RuleModel::select();

        $tree = new Tree();
        $data = $tree->tree($data, 'title');
        //去掉子栏目（防止，选择子分类,当前栏目）
        $sdata = $tree->channelList($data, $id);
        foreach ($sdata as $key => $v) {
            unset($data[$v['id']]);
        }
        //unset($data[$id]);
        //print_r($data);
        $this->assign([
            'typename' => $data,
            'info'     => $info,
            'typepid'  => $info['pid'],
        ]);
        return $this->fetch();
    }

    /*
    信息删去
     */
    public function delete($value = '')
    {
        $id   = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data = RuleModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'admin/AuthRule/index', '', 1);
            }
        }
        return $this->error('删去失败');
    }

    /**
     * 快捷操作
     * @return [type] [description]
     */
    public function quickedit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {
            if (input('?post.sorts')) {
                $data = ['id' => Request::post('id/d'), 'sorts' => Request::post('sorts')]; //排序
            } else if (input('?post.status')) {
                $data = ['id' => Request::post('id/d'), 'status' => Request::post('status')]; //状态
            } else if (input('?post.ismenu')) {
                $data = ['id' => Request::post('id/d'), 'ismenu' => Request::post('ismenu')]; //菜单
            }
            $model = RuleModel::update($data);
            if (!empty($model)) {
                return $this->success('修改成功');
            } else {
                return $this->error('修改失败');
            }
        }
    }

}
